<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
            "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
	<head>
		<title>Model Validations Demo</title>
        <style type='text/css'>
            body {font-family: verdana}
            li {border: solid 1px gray; padding: 5px; width: 250px;}
			li a {color: red; font-weight: bold;}
			p {width: 400px;}
        </style>
	</head>
	<body>
<div id="demo-instructions">
		<h1>Model Validations Demo</h1>
		<p>This demo demonstrates using validations to prevent
		a person's birthday from being in the future.</p>
		<p>Clicking a person's name will show a form to update
		their birthday.  Change the birthday and 'blur' the 
		input to update their listed age.</p>
</div>
<div id="demo-html">
<div id='contacts1'></div>
<div id='update'></div>
</div>
<script type='text/javascript' 
        src='../../../steal/steal.js'>   
</script>
<script type='text/javascript'>
steal.plugins('jquery/model',
		'jquery/dom/fixture',
		'jquery/model/list',
		'jquery/model/validations').start()
</script>
<script type='text/javascript'>
	CONVERSIONS = {
		date : function(raw){
			if(typeof raw == 'string'){
				var matches = raw.match(/(\d+)-(\d+)-(\d+)/)
				return new Date( +matches[1], 
				                 (+matches[2])-1, 
				                 +matches[3] )
			}else if(raw instanceof Date){
				return raw;
			}
		}
	}
	FIXTURE = function(){
				return [[{'id': 1,'name' : 'Justin Meyer','birthday': '1982-10-20'},
					 {'id': 2,'name' : 'Brian Moschel','birthday': '1983-11-10'},
					 {'id': 3,'name' : 'Alex Gomes','birthday': '1980-2-10'}]];
			}
</script>
<script type='text/javascript'  id="demo-source">   

$.Model.extend("Contact",{
	init : function(){
		this.validate("birthday",function(){
			if(this.birthday > new Date){
				return "your birthday needs to be in the past"
			}
		})
	},
	attributes : { 
		birthday : 'date'
	},
	convert : CONVERSIONS,
	findAll : function(params, success, error){
		$.get("/recipes.json",{},
			this.callback(['wrapMany',success]),
			"json",FIXTURE )
	},
	update : function(id, attrs, success, error){
		$.post("/recipes.json",{},success,'json',function(){
			return [attrs]
		})
	}
},{
	ageThisYear : function(){
		return new Date().getFullYear() - 
		      this.birthday.getFullYear()
	},
	getBirthday : function(){
		return ""+this.birthday.getFullYear()+
			"-"+(this.birthday.getMonth()+1)+
			"-"+this.birthday.getDate();
	}

});

makeAgeUpdater = function(contact){
	var updater = $("#update")
	updater.html("");
	updater.append("<span>" +contact.name+"'s birthday </span>")
	$('<input/>').val(contact.attr("birthday")).change(function(){
		
		
		contact.attr("birthday", this.value , function(){
			$('#error').hide();
		}, function(errors){
			$('#error').html(errors.birthday[0]).show();
		})
	}).appendTo(updater);
	updater.append("<div id='error' style='display:none'><div>");
}


</script>

<script type='text/javascript'>
	// List 1
Contact.findAll({},function(contacts){
  var contactsEl = $('#contacts1');
  $.each(contacts, function(i, contact){
    var li = $('<li>')
              .model(contact)
              .html(contact.name+" "+contact.ageThisYear()+
                    " <a>Show</a>")
              .appendTo(contactsEl);
    contact.bind("birthday", function(){
      li.html(contact.name+" "+this.ageThisYear()+
              " <a>Show</a>");
    })
  })
  
  contactsEl.delegate("li","click", function(){
	 makeAgeUpdater( $(this).closest('.contact').model() );
  });
});
</script>
	</body>
</html>